package common;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;


import org.jconfig.ConfigurationManager;
import org.jconfig.handler.XMLFileHandler;

public class AppConfig {
	
//	Tao file cau hinh mac dinh
	public void creatDefaultFileConfig(File file) {
		BufferedWriter bw;
		try {
			bw = new BufferedWriter(new FileWriter(file));
			bw.write("<?xml version=\"1.0\" ?>");
            bw.newLine();
            bw.write("\t<properties>");
            bw.newLine();
            bw.write("\t\t<category name=\"JDBC\">");
            bw.newLine();
            bw.write("\t\t\t<property name=\"URL\" value=\"jdbc:oracle:thin:@(description=(address=(host=192.168.6.112)(protocol=tcp)(port=1521))(connect_data=(SERVER = DEDICATED)(service_name=gstt)))\"/>");
            bw.newLine();
            bw.write("\t\t\t<property name=\"USERNAME\" value=\"dv\"/>");
            bw.newLine();
            bw.write("\t\t\t<property name=\"PASSWORD\" value=\"dv\"/>");
            bw.newLine();
            bw.write("\t\t\t<property name=\"MAX_LIMIT\" value=\"4\"/>");
            bw.newLine();
            bw.write("\t\t\t<property name=\"AUTO_COMMIT\" value=\"false\"/>");
            bw.newLine();
            bw.write("\t\t</category>");
            bw.newLine();
            bw.write("\t</properties>");
            bw.flush();
            bw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void reloadConfig(){
		ConfigurationManager cm = ConfigurationManager.getInstance();
		try {
			String filePath = Constant.CONFIG_FOLDER;
			
//			Tao thu muc config
			File file = new File(filePath);
			if (!file.exists()) {
				file.mkdirs();
			}
			
//			Tao file config
			filePath += "MutilThreadConfig.xml";
			file = new File(filePath);
			if (!file.exists()) {
				creatDefaultFileConfig(file);
			}
			
//			Xy ly file config
			XMLFileHandler fileHandler = new XMLFileHandler();
			fileHandler.setFile(file);
			cm.load(fileHandler, "MutilThreadConfig.xml");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
